package com.snailthink.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @program: springmvc-study
 * @description:
 * @author: SnailThink
 * @create: 2021-03-12 14:21
 **/
@Controller
public class RestFulController {

	/**
	 * 原来的：http:localhost:8708/add?a=1&b=2
	 * RESTful：http:localhost:8708/add/a/b
	 * @param a
	 * @param b
	 * @param model
	 * @return
	 */
	@RequestMapping("/add")
	public String add(int a, int b, Model model) {
		int result = a + b;
		model.addAttribute("msg", result);
		return "test";
	}

	/**
	 *
	 *  value = "/commit/{a}/{b}
	 *  path = "/commit/{a}/{b}
	 *
	 * @param a
	 * @param b
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/commit/{a}/{b}",method = RequestMethod.GET)
	public String resultAdd(@PathVariable int a, @PathVariable int b, Model model) {
		int result = a + b;
		model.addAttribute("msg", result);
		return "test";
	}

	/**
	 * RestFul 风格
	 * @param p1
	 * @param p2
	 * @param model
	 * @return
	 */
	//映射访问路径
	@RequestMapping("/commit/{p1}/{p2}")
	public String index(@PathVariable int p1, @PathVariable int p2, Model model){

		int result = p1+p2;
		//Spring MVC会自动实例化一个Model对象用于向视图中传值
		model.addAttribute("msg", "结果："+result);
		//返回视图位置
		return "test";
	}

	//映射访问路径,必须是Get请求
	@RequestMapping(value = "/hello",method = {RequestMethod.DELETE})
	public String index2(Model model){
		model.addAttribute("msg", "hello!");
		return "test";
	}


	@GetMapping(value = "/hello")
	public String index3(Model model){
		model.addAttribute("msg", "hello!");
		return "test";
	}

}
